package net.m510.autosurfer.smbcvisa;

import java.io.File;
import java.net.URI;

import net.m510.autosurfer.common.FileGetProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;

import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class SmbcVisaDownload extends FileGetProcess {
	public SmbcVisaDownload() {
		this(null,null,null);
	}
	
	public SmbcVisaDownload(CloseableHttpClient client, HttpClientContext context, File dir) {
		super(client, context, dir, null);
	}

	@Override
	protected URI getUri(Document doc) {
		final String host = "https://www.smbc-card.com/";
		// CSVダウンロードのURLを取得
		Elements e = doc.getElementsContainingOwnText("CSV形式で保存");
		if ( e.isEmpty() ) throw new InvalidDocumentException("CSV形式で保存のURLが見つかりません",doc);
		Element dl_anchor = e.first();
		log.fine("CSV DLへのアンカー "+dl_anchor);
		URI uri = URI.create(host);
		uri = uri.resolve(dl_anchor.attr("href"));
		return uri;
	}
}
